home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / mx / rpc / http / HTTPService.as
Encoding:
Text File  |  2008-05-21  |  17.1 KB  |  529 lines

  1. package mx.rpc.http
  2. {
  3.    import flash.utils.getQualifiedClassName;
  4.    import flash.xml.XMLDocument;
  5.    import flash.xml.XMLNode;
  6.    import mx.collections.ArrayCollection;
  7.    import mx.core.mx_internal;
  8.    import mx.messaging.ChannelSet;
  9.    import mx.messaging.channels.DirectHTTPChannel;
  10.    import mx.messaging.config.LoaderConfig;
  11.    import mx.messaging.messages.HTTPRequestMessage;
  12.    import mx.messaging.messages.IMessage;
  13.    import mx.rpc.AbstractInvoker;
  14.    import mx.rpc.AsyncDispatcher;
  15.    import mx.rpc.AsyncRequest;
  16.    import mx.rpc.AsyncToken;
  17.    import mx.rpc.Fault;
  18.    import mx.rpc.events.FaultEvent;
  19.    import mx.rpc.xml.SimpleXMLDecoder;
  20.    import mx.rpc.xml.SimpleXMLEncoder;
  21.    import mx.utils.ObjectProxy;
  22.    import mx.utils.StringUtil;
  23.    import mx.utils.Translator;
  24.    import mx.utils.URLUtil;
  25.    
  26.    use namespace mx_internal;
  27.    
  28.    public class HTTPService extends AbstractInvoker
  29.    {
  30.       private static var _directChannelSet:ChannelSet;
  31.       
  32.       public static const RESULT_FORMAT_E4X:String = "e4x";
  33.       
  34.       public static const RESULT_FORMAT_FLASHVARS:String = "flashvars";
  35.       
  36.       public static const RESULT_FORMAT_OBJECT:String = "object";
  37.       
  38.       public static const RESULT_FORMAT_ARRAY:String = "array";
  39.       
  40.       public static const RESULT_FORMAT_TEXT:String = "text";
  41.       
  42.       public static const RESULT_FORMAT_XML:String = "xml";
  43.       
  44.       public static const CONTENT_TYPE_XML:String = "application/xml";
  45.       
  46.       public static const CONTENT_TYPE_FORM:String = "application/x-www-form-urlencoded";
  47.       
  48.       public static const DEFAULT_DESTINATION_HTTP:String = "DefaultHTTP";
  49.       
  50.       public static const DEFAULT_DESTINATION_HTTPS:String = "DefaultHTTPS";
  51.       
  52.       public static const ERROR_URL_REQUIRED:String = "Client.URLRequired";
  53.       
  54.       public static const ERROR_DECODING:String = "Client.CouldNotDecode";
  55.       
  56.       public static const ERROR_ENCODING:String = "Client.CouldNotEncode";
  57.       
  58.       private static const TRANSLATOR:Translator = Translator.getDefaultInstanceFor(HTTPService);
  59.       
  60.       public var xmlDecode:Function;
  61.       
  62.       public var xmlEncode:Function;
  63.       
  64.       public var headers:Object;
  65.       
  66.       public var request:Object;
  67.       
  68.       private var _useProxy:Boolean = false;
  69.       
  70.       private var _url:String;
  71.       
  72.       public var contentType:String = "application/x-www-form-urlencoded";
  73.       
  74.       public var method:String = "GET";
  75.       
  76.       private var _resultFormat:String = "object";
  77.       
  78.       mx_internal var _rootURL:String;
  79.       
  80.       public function HTTPService(param1:String = null)
  81.       {
  82.          contentType = CONTENT_TYPE_FORM;
  83.          headers = {};
  84.          method = HTTPRequestMessage.GET_METHOD;
  85.          request = {};
  86.          _resultFormat = RESULT_FORMAT_OBJECT;
  87.          _useProxy = false;
  88.          super();
  89.          mx_internal::asyncRequest = new AsyncRequest();
  90.          makeObjectsBindable = true;
  91.          if(URLUtil.isHttpsURL(LoaderConfig.url))
  92.          {
  93.             mx_internal::asyncRequest.destination = DEFAULT_DESTINATION_HTTPS;
  94.          }
  95.          else
  96.          {
  97.             mx_internal::asyncRequest.destination = DEFAULT_DESTINATION_HTTP;
  98.          }
  99.       }
  100.       
  101.       public function get resultFormat() : String
  102.       {
  103.          return _resultFormat;
  104.       }
  105.       
  106.       public function send(param1:Object = null) : AsyncToken
  107.       {
  108.          var _loc2_:Object = null;
  109.          var _loc3_:AsyncToken = null;
  110.          var _loc4_:Fault = null;
  111.          var _loc5_:FaultEvent = null;
  112.          var _loc6_:HTTPRequestMessage = null;
  113.          var _loc7_:Object = null;
  114.          var _loc8_:SimpleXMLEncoder = null;
  115.          var _loc9_:XMLDocument = null;
  116.          var _loc10_:Array = null;
  117.          var _loc11_:int = 0;
  118.          var _loc12_:Object = null;
  119.          var _loc13_:String = null;
  120.          var _loc14_:ChannelSet = null;
  121.          if(param1 == null)
  122.          {
  123.             param1 = request;
  124.          }
  125.          if(contentType == CONTENT_TYPE_XML)
  126.          {
  127.             if(!(param1 is XMLNode) && !(param1 is XML))
  128.             {
  129.                if(xmlEncode != null)
  130.                {
  131.                   _loc7_ = xmlEncode(param1);
  132.                   if(null == _loc7_)
  133.                   {
  134.                      _loc3_ = new AsyncToken(null);
  135.                      _loc4_ = new Fault(ERROR_ENCODING,TRANSLATOR.textOf("xmlEncodeReturnNull"));
  136.                      _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_);
  137.                      new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10);
  138.                      return _loc3_;
  139.                   }
  140.                   if(!(_loc7_ is XMLNode))
  141.                   {
  142.                      _loc3_ = new AsyncToken(null);
  143.                      _loc4_ = new Fault(ERROR_ENCODING,TRANSLATOR.textOf("xmlEncodeReturnNoXMLNode"));
  144.                      _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_);
  145.                      new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10);
  146.                      return _loc3_;
  147.                   }
  148.                   _loc2_ = XMLNode(_loc7_).toString();
  149.                }
  150.                else
  151.                {
  152.                   _loc8_ = new SimpleXMLEncoder(null);
  153.                   _loc9_ = new XMLDocument();
  154.                   _loc10_ = _loc8_.encodeValue(param1,new QName(null,"encoded"),new XMLNode(1,"top")).childNodes.concat();
  155.                   _loc11_ = 0;
  156.                   while(_loc11_ < _loc10_.length)
  157.                   {
  158.                      _loc9_.appendChild(_loc10_[_loc11_]);
  159.                      _loc11_++;
  160.                   }
  161.                   _loc2_ = _loc9_.toString();
  162.                }
  163.             }
  164.             else
  165.             {
  166.                _loc2_ = param1.toString();
  167.             }
  168.          }
  169.          else if(contentType == CONTENT_TYPE_FORM)
  170.          {
  171.             _loc2_ = {};
  172.             for(_loc13_ in param1)
  173.             {
  174.                _loc12_ = param1[_loc13_];
  175.                if(_loc12_ != null)
  176.                {
  177.                   _loc2_[_loc13_] = _loc12_ is Object ? _loc12_.toString() : _loc12_;
  178.                }
  179.             }
  180.          }
  181.          else
  182.          {
  183.             _loc2_ = param1;
  184.          }
  185.          _loc6_ = new HTTPRequestMessage();
  186.          if(useProxy)
  187.          {
  188.             if(Boolean(url) && url != "")
  189.             {
  190.                _loc6_.url = URLUtil.getFullURL(rootURL,url);
  191.             }
  192.          }
  193.          else
  194.          {
  195.             if(!url)
  196.             {
  197.                _loc3_ = new AsyncToken(null);
  198.                _loc4_ = new Fault(ERROR_URL_REQUIRED,TRANSLATOR.textOf("urlNotSpecified"));
  199.                _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_);
  200.                new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10);
  201.                return _loc3_;
  202.             }
  203.             if(!useProxy)
  204.             {
  205.                _loc14_ = getDirectChannelSet();
  206.                if(_loc14_ != mx_internal::asyncRequest.channelSet)
  207.                {
  208.                   mx_internal::asyncRequest.channelSet = _loc14_;
  209.                }
  210.             }
  211.             _loc6_.url = url;
  212.          }
  213.          _loc6_.contentType = contentType;
  214.          _loc6_.method = method.toUpperCase();
  215.          if(contentType == CONTENT_TYPE_XML && _loc6_.method == HTTPRequestMessage.GET_METHOD)
  216.          {
  217.             _loc6_.method = HTTPRequestMessage.POST_METHOD;
  218.          }
  219.          _loc6_.body = _loc2_;
  220.          _loc6_.httpHeaders = headers;
  221.          return mx_internal::invoke(_loc6_);
  222.       }
  223.       
  224.       public function logout() : void
  225.       {
  226.          mx_internal::asyncRequest.logout();
  227.       }
  228.       
  229.       private function decodeArray(param1:Object) : Object
  230.       {
  231.          var _loc2_:Array = null;
  232.          if(param1 is Array)
  233.          {
  234.             _loc2_ = param1 as Array;
  235.          }
  236.          else
  237.          {
  238.             if(param1 is ArrayCollection)
  239.             {
  240.                return param1;
  241.             }
  242.             _loc2_ = [];
  243.             _loc2_.push(param1);
  244.          }
  245.          if(makeObjectsBindable)
  246.          {
  247.             return new ArrayCollection(_loc2_);
  248.          }
  249.          return _loc2_;
  250.       }
  251.       
  252.       public function get destination() : String
  253.       {
  254.          return mx_internal::asyncRequest.destination;
  255.       }
  256.       
  257.       public function setRemoteCredentials(param1:String, param2:String) : void
  258.       {
  259.          mx_internal::asyncRequest.setRemoteCredentials(param1,param2);
  260.       }
  261.       
  262.       public function set destination(param1:String) : void
  263.       {
  264.          useProxy = true;
  265.          mx_internal::asyncRequest.destination = param1;
  266.       }
  267.       
  268.       public function disconnect() : void
  269.       {
  270.          mx_internal::asyncRequest.disconnect();
  271.       }
  272.       
  273.       public function set requestTimeout(param1:int) : void
  274.       {
  275.          if(mx_internal::asyncRequest.requestTimeout != param1)
  276.          {
  277.             mx_internal::asyncRequest.requestTimeout = param1;
  278.          }
  279.       }
  280.       
  281.       private function getDirectChannelSet() : ChannelSet
  282.       {
  283.          var _loc1_:ChannelSet = null;
  284.          if(_directChannelSet == null)
  285.          {
  286.             _loc1_ = new ChannelSet();
  287.             _loc1_.addChannel(new DirectHTTPChannel("direct_http_channel"));
  288.             _directChannelSet = _loc1_;
  289.          }
  290.          return _directChannelSet;
  291.       }
  292.       
  293.       public function get useProxy() : Boolean
  294.       {
  295.          return _useProxy;
  296.       }
  297.       
  298.       public function get rootURL() : String
  299.       {
  300.          if(mx_internal::_rootURL == null)
  301.          {
  302.             mx_internal::_rootURL = LoaderConfig.url;
  303.          }
  304.          return mx_internal::_rootURL;
  305.       }
  306.       
  307.       public function setCredentials(param1:String, param2:String) : void
  308.       {
  309.          mx_internal::asyncRequest.setCredentials(param1,param2);
  310.       }
  311.       
  312.       public function set channelSet(param1:ChannelSet) : void
  313.       {
  314.          useProxy = true;
  315.          mx_internal::asyncRequest.channelSet = param1;
  316.       }
  317.       
  318.       private function decodeParameterString(param1:String) : Object
  319.       {
  320.          var _loc2_:String = null;
  321.          var _loc3_:Array = null;
  322.          var _loc4_:Object = null;
  323.          var _loc5_:int = 0;
  324.          var _loc6_:String = null;
  325.          var _loc7_:int = 0;
  326.          var _loc8_:String = null;
  327.          var _loc9_:String = null;
  328.          _loc2_ = StringUtil.trim(param1);
  329.          _loc3_ = _loc2_.split("&");
  330.          _loc4_ = {};
  331.          _loc5_ = 0;
  332.          while(_loc5_ < _loc3_.length)
  333.          {
  334.             _loc6_ = _loc3_[_loc5_];
  335.             _loc7_ = int(_loc6_.indexOf("="));
  336.             if(_loc7_ != -1)
  337.             {
  338.                _loc8_ = unescape(_loc6_.substr(0,_loc7_));
  339.                _loc8_ = _loc8_.split("+").join(" ");
  340.                _loc9_ = unescape(_loc6_.substr(_loc7_ + 1));
  341.                _loc9_ = _loc9_.split("+").join(" ");
  342.                _loc4_[_loc8_] = _loc9_;
  343.             }
  344.             _loc5_++;
  345.          }
  346.          return _loc4_;
  347.       }
  348.       
  349.       public function set useProxy(param1:Boolean) : void
  350.       {
  351.          var _loc2_:ChannelSet = null;
  352.          if(param1 != _useProxy)
  353.          {
  354.             _useProxy = param1;
  355.             _loc2_ = getDirectChannelSet();
  356.             if(!useProxy)
  357.             {
  358.                if(_loc2_ != mx_internal::asyncRequest.channelSet)
  359.                {
  360.                   mx_internal::asyncRequest.channelSet = _loc2_;
  361.                }
  362.             }
  363.             else if(mx_internal::asyncRequest.channelSet == _loc2_)
  364.             {
  365.                mx_internal::asyncRequest.channelSet = null;
  366.             }
  367.          }
  368.       }
  369.       
  370.       public function get requestTimeout() : int
  371.       {
  372.          return mx_internal::asyncRequest.requestTimeout;
  373.       }
  374.       
  375.       public function get channelSet() : ChannelSet
  376.       {
  377.          return mx_internal::asyncRequest.channelSet;
  378.       }
  379.       
  380.       public function set url(param1:String) : void
  381.       {
  382.          _url = param1;
  383.       }
  384.       
  385.       public function set resultFormat(param1:String) : void
  386.       {
  387.          switch(param1)
  388.          {
  389.             case RESULT_FORMAT_OBJECT:
  390.             case RESULT_FORMAT_ARRAY:
  391.             case RESULT_FORMAT_XML:
  392.             case RESULT_FORMAT_E4X:
  393.             case RESULT_FORMAT_TEXT:
  394.             case RESULT_FORMAT_FLASHVARS:
  395.                _resultFormat = param1;
  396.                return;
  397.             default:
  398.                throw new ArgumentError(TRANSLATOR.textOf("invalidResultFormat",param1,RESULT_FORMAT_OBJECT,RESULT_FORMAT_ARRAY,RESULT_FORMAT_XML,RESULT_FORMAT_E4X,RESULT_FORMAT_TEXT,RESULT_FORMAT_FLASHVARS));
  399.          }
  400.       }
  401.       
  402.       public function set rootURL(param1:String) : void
  403.       {
  404.          mx_internal::_rootURL = param1;
  405.       }
  406.       
  407.       override mx_internal function processResult(param1:IMessage, param2:AsyncToken) : Boolean
  408.       {
  409.          var body:Object = null;
  410.          var tmp:Object = null;
  411.          var fault:Fault = null;
  412.          var decoded:Object = null;
  413.          var fault1:Fault = null;
  414.          var decoder:SimpleXMLDecoder = null;
  415.          var fault2:Fault = null;
  416.          var fault3:Fault = null;
  417.          var message:IMessage = param1;
  418.          var token:AsyncToken = param2;
  419.          body = message.body;
  420.          if(body == null || body != null && body is String && StringUtil.trim(String(body)) == "")
  421.          {
  422.             mx_internal::_result = body;
  423.             return true;
  424.          }
  425.          if(body is String)
  426.          {
  427.             if(resultFormat == RESULT_FORMAT_XML || resultFormat == RESULT_FORMAT_OBJECT || resultFormat == RESULT_FORMAT_ARRAY)
  428.             {
  429.                tmp = new XMLDocument();
  430.                XMLDocument(tmp).ignoreWhite = true;
  431.                try
  432.                {
  433.                   XMLDocument(tmp).parseXML(String(body));
  434.                }
  435.                catch(parseError:Error)
  436.                {
  437.                   fault = new Fault(ERROR_DECODING,parseError.message);
  438.                   mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault,token,message));
  439.                   return false;
  440.                }
  441.                if(resultFormat == RESULT_FORMAT_OBJECT || resultFormat == RESULT_FORMAT_ARRAY)
  442.                {
  443.                   if(xmlDecode != null)
  444.                   {
  445.                      decoded = xmlDecode(tmp);
  446.                      if(decoded == null)
  447.                      {
  448.                         fault1 = new Fault(ERROR_DECODING,TRANSLATOR.textOf("xmlDecodeReturnNull"));
  449.                         mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault1,token,message));
  450.                      }
  451.                   }
  452.                   else
  453.                   {
  454.                      decoder = new SimpleXMLDecoder(makeObjectsBindable);
  455.                      decoded = decoder.decodeXML(XMLNode(tmp));
  456.                      if(decoded == null)
  457.                      {
  458.                         fault2 = new Fault(ERROR_DECODING,TRANSLATOR.textOf("defaultDecoderFailed"));
  459.                         mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault2,token,message));
  460.                      }
  461.                   }
  462.                   if(decoded == null)
  463.                   {
  464.                      return false;
  465.                   }
  466.                   if(makeObjectsBindable && getQualifiedClassName(decoded) == "Object")
  467.                   {
  468.                      decoded = new ObjectProxy(decoded);
  469.                   }
  470.                   else
  471.                   {
  472.                      ;
  473.                   }
  474.                   if(resultFormat == RESULT_FORMAT_ARRAY)
  475.                   {
  476.                      decoded = decodeArray(decoded);
  477.                   }
  478.                   mx_internal::_result = decoded;
  479.                }
  480.                else
  481.                {
  482.                   if(tmp.childNodes.length == 1)
  483.                   {
  484.                      tmp = tmp.firstChild;
  485.                   }
  486.                   mx_internal::_result = tmp;
  487.                }
  488.             }
  489.             else if(resultFormat == RESULT_FORMAT_E4X)
  490.             {
  491.                try
  492.                {
  493.                   mx_internal::_result = new XML(String(body));
  494.                }
  495.                catch(error:Error)
  496.                {
  497.                   fault3 = new Fault(ERROR_DECODING,error.message);
  498.                   mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault3,token,message));
  499.                   return false;
  500.                }
  501.             }
  502.             else if(resultFormat == RESULT_FORMAT_FLASHVARS)
  503.             {
  504.                mx_internal::_result = decodeParameterString(String(body));
  505.             }
  506.             else
  507.             {
  508.                mx_internal::_result = body;
  509.             }
  510.          }
  511.          else
  512.          {
  513.             if(resultFormat == RESULT_FORMAT_ARRAY)
  514.             {
  515.                body = decodeArray(body);
  516.             }
  517.             mx_internal::_result = body;
  518.          }
  519.          return true;
  520.       }
  521.       
  522.       public function get url() : String
  523.       {
  524.          return _url;
  525.       }
  526.    }
  527. }
  528.  
  529.